#!/usr/bin/python
#Auther : MillionLee

'''
Get variety of timelines

Get variety of timelines
'''

from doOAuthUpdate import OAuthUpdate

from _testkey import *

class GetUserTimeline(OAuthUpdate):
    def __init__(self):
        """ constructor """
        OAuthUpdate.__init__(self)
        
    def getUserLastUpdateTime(self):
        """ return user's last update time in str "%m%d%y" """
        #get the lasted post
        userLastPosts = self.api.user_timeline(count=1, page=1)
        if len(userLastPosts) == 0:
            return "000000"
        else:
            self.obj = userLastPosts[0]
        
        #get update time
        uesrLastUpdateStrWeibo = str(self.getAtt("created_at"))
        return uesrLastUpdateStrWeibo[5:7] + uesrLastUpdateStrWeibo[8:10] + uesrLastUpdateStrWeibo[2:4]
        #without timezone trans, default is +8 in weibo

    def getUserLastOriginalUpdateTime(self):
        """ return user's last original post time in str "%m%d%y" """
        #find the lasted original post
        pageCount = 1;
        while pageCount < 11:
        #find in the last 20*10 = 200 posts
            userLastPosts = self.api.user_timeline(count = 20, page = pageCount)
            
            for post in userLastPosts:
                try:
                    post.__getattribute__("retweeted_status")
                except Exception:
                #be original
                    self.obj = post
                    pageCount = 11
                    break;
            pageCount += 1
        
        #get update time
        uesrLastUpdateStrWeibo = str(self.getAtt("created_at"))
        return uesrLastUpdateStrWeibo[5:7] + uesrLastUpdateStrWeibo[8:10] + uesrLastUpdateStrWeibo[2:4]
        #without timezone trans, default is +8 in weibo

if __name__ == "__main__":
    test = GetUserTimeline()
    test.setToken(KEY_LEE['KEY'], KEY_LEE['SECRET'])
    print test.getUserLastOriginalUpdateTime()

